package com.lanf.generator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author tanlingfei
 * @version 1.0
 * @description TODO
 * @date 2023/5/17 22:55
 */
public class Utils {
    private static Pattern linePattern = Pattern.compile("_(\\w)");
    private static Pattern humpPattern = Pattern.compile("[A-Z]");


    //数据库类型对应到java中的类型 可根据自己的需要调整
    public static String dataToProperty(String columnType) {
        //转换为java数据类型
        if ("TINYINT".equalsIgnoreCase(columnType) || "INT".equalsIgnoreCase(columnType)
                || columnType.indexOf("INT") > -1 || columnType.indexOf("int") > -1) {
            columnType = "Integer";
        } else if ("BIGINT".equalsIgnoreCase(columnType)
                    || "VARCHAR".equalsIgnoreCase(columnType)
                    || "CHAR".equalsIgnoreCase(columnType)
                    || "text".equalsIgnoreCase(columnType)
                    || "tinytext".equalsIgnoreCase(columnType)) {
                columnType = "String";
            } else if ("double".equalsIgnoreCase(columnType) || "float".equalsIgnoreCase(columnType)
                    || "decimal".equalsIgnoreCase(columnType) || "numeric".equalsIgnoreCase(columnType)
                    || columnType.indexOf("DECIMAL") > -1) {
                columnType = "java.math.BigDecimal";
            } else if ("DATETIME".equalsIgnoreCase(columnType) || "timestamp".equalsIgnoreCase(columnType) || "time".equalsIgnoreCase(columnType)) {
                columnType = "java.util.Date";
        } else {
            columnType = "String";
        }
        return columnType;
    }

    /**
     * 下划线转驼峰
     */
    public static String lineToHump(String str) {
        if (str == null) {
            return str;
        }
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 驼峰转下划线
     */
    public static String humpToLine2(String str) {
        if (str == null) {
            return str;
        }
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String toFirstUpper(String str) {
        return (str != null && !"".equals(str) && str.length() > 0) ? (str.substring(0, 1).toUpperCase() + (str.length() > 1 ? str.substring(1) : "")) : "";
    }

    public static String toFirstLower(String str) {
        return (str != null && !"".equals(str) && str.length() > 0) ? (str.substring(0, 1).toLowerCase() + (str.length() > 1 ? str.substring(1) : "")) : "";
    }

    public static void main(String[] args) {
//        String str = "dept_id";
//        System.out.println(Utils.lineToHump(str));
//        System.out.println("sys_333".split("_")[0]);
        System.out.println(Utils.lineToHump("sys_dic_item"));
        String str2 = "{\"dicCode\":\"1000\"}";
    }
}
